package com.example.studentmanagement;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.studentmanagement.dao.StudentDao;
import com.example.studentmanagement.entity.Student;
import com.example.studentmanagement.util.DBUtil;
import com.example.studentmanagement.view.Bottom_Bar;
import com.example.studentmanagement.view.Top_Bar;


public class PersonalActivity extends AppCompatActivity {
    private StudentDao studentdao ;
    private Top_Bar top_bar ;
    private Bottom_Bar bottom_bar ;
    private RelativeLayout per_linear ;
    private RelativeLayout exitLogin ;
    private TextView per_name ;
    private TextView per_id ;
    private RelativeLayout per_chooseCourse ;
    private RelativeLayout per_setting ;

    @SuppressLint({"MissingInflatedId", "SetTextI18n"})
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal);

        DBUtil dbUtil = new DBUtil(PersonalActivity.this);
        SQLiteDatabase db = dbUtil.getWritableDatabase();
        DBUtil.db=db;

        studentdao = new StudentDao();


        top_bar = findViewById(R.id.personal_top);
        bottom_bar = findViewById(R.id.personal_bottom);
        per_linear=findViewById(R.id.personal_myinfo);
        exitLogin=findViewById(R.id.personal_exitLogin);
        per_name = findViewById(R.id.personal_name);
        per_id = findViewById(R.id.personal_id) ;
        per_chooseCourse=findViewById(R.id.personal_choosecurriculum);
        per_setting = findViewById(R.id.personal_setting);


        //获取登录页面传来的id和name
        SharedPreferences sharedPreferences = getSharedPreferences("loginId" , Context.MODE_PRIVATE);
        String studentId = sharedPreferences.getString("s_id" , "");
        Log.d("111111111111111111111", "onCreate: ---------"+studentId);
        Student student = studentdao.getStudentById(studentId);

        top_bar.setLeftBtnNoVisible(true);//不显示
        top_bar.setRightBtnNoVisible(true);
        top_bar.setTitleText("我的");
        bottom_bar.setPersonBtnBackground(R.color.q2);
        bottom_bar.setStudentBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PersonalActivity.this , StudentListActivity.class) ;
                startActivity(intent);
                finish();
            }
        });
        bottom_bar.setCourseBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PersonalActivity.this , CurriculumActivity.class) ;
                startActivity(intent);
                finish();
            }
        });
        per_name.setText(student.getName());
        per_id.setText(studentId);
        per_linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PersonalActivity.this , StudentInformationActivity.class);
                intent.putExtra("s_id", studentId);
                startActivity(intent);
            }
        });
        per_chooseCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PersonalActivity.this , ChooseCurriculum.class);
                startActivity(intent);
            }
        });
        per_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PersonalActivity.this , SettingActivity.class);
                startActivity(intent);
            }
        });
        exitLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLogoutConfirmationDialog();
            }
        });

    }
    private void showLogoutConfirmationDialog() {
        new AlertDialog.Builder(this)
                .setMessage("确定要退出登录吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 用户点击确定，执行退出登录操作
                        logout();
                    }
                })
                .setNegativeButton("取消", null) // 点击取消，对话框消失，不执行任何操作
                .show();
    }

    private void logout() {
        // 关闭所有Activity并回到登录界面
        // 获取当前Activity的Intent
        Intent loginIntent = new Intent(PersonalActivity.this, MainActivity.class);
        // 确保使用FLAG_ACTIVITY_CLEAR_TOP，这样当新的Intent启动时，如果在任务栈中有该Activity的实例，则不会重新创建，而是重用这个Activity实例
        loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // 使用finishAffinity来关闭所有栈顶的Activity
        finishAffinity();
        // 使用startActivity启动登录Activity
        startActivity(loginIntent);
        // 如果需要，可以在这里添加一个成功退出登录的Toast提示或其他反馈
    }
}